package ControllerSrevlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MYJDBC.jdbc;
import User_index.User_Login;
import User_index.User_dao;

/**
 * Servlet implementation class User_ToLogin
 */
@WebServlet("/User_ToLogin")
public class User_ToLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public User_ToLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String UserNumber = request.getParameter("username");
		String UserPasd = request.getParameter("pass");
		User_dao user = new User_dao();
		jdbc jc = new jdbc();
		request.setAttribute("SqlUser",jc.getSqlUser());
		request.setAttribute("SqlPass",jc.getSqlPass());
		request.setAttribute("SqlName", jc.getSqlName());
		User_Login login = new User_Login();
		if(UserNumber=="") {
			request.setAttribute("userlogintext", "请填写账号和密码");
			request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
		}else {
			try {
				user = login.UserLogin(UserNumber);
				if(user.getUser_number()!=null) {
					if(user.getUser_number().equals(UserNumber)&&user.getUser_pasd().equals(UserPasd)) {
						try {
							ServletContext context = this.getServletContext();
							context.setAttribute("user", user);
							response.sendRedirect("/Music_web/BlueVideo");
						} catch (Exception e) {
							e.printStackTrace();
						}
						
					}else {
						
						if(!user.getUser_pasd().equals(UserPasd)) {
							request.setAttribute("userlogintext", "账号密码不正确");
							request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
						}else {
							request.setAttribute("userlogintext", "遇到未知错误请联系管理员");
							request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
						}
						
					}
				}else {
					request.setAttribute("userlogintext", "账号不存在");
					request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

}
